using Godot;
using System;

public partial class Scene1Node : Node2D
{
    private BlockGenerator _blockGenerator;
    private Label _label;
    private Area2D _area;
    private int _missCount = 0;
    private int _score = 0;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        base._Ready();
        _blockGenerator = GetNode<BlockGenerator>("BlockGenerator");
        if (_blockGenerator == null)
            GD.Print("Scene1Node._Ready: blockGenerator is null");

        _area = GetNode<Area2D>(@"Keys/Area2D");

        _label = GetNode<Label>("Label");
        UpdateLabel();
        //_area.AreaShapeEntered += _area_AreaShapeEntered;
        _area.AreaShapeExited += _area_AreaShapeExited;
    }

    private void _area_AreaShapeExited(Rid areaRid, Area2D area, long areaShapeIndex, long localShapeIndex)
    {
        if (area is TrackBlock trackBlock)
        {
            if (trackBlock.IsCompleted == false)
            {
                _missCount++;
                UpdateLabel();
            }
        }

    }

    protected override void Dispose(bool disposing)
    {
        _area.AreaShapeExited -= _area_AreaShapeExited;
        //_area.AreaShapeEntered -= _area_AreaShapeEntered;
        base.Dispose(disposing);
    }

    //private void _area_AreaShapeEntered(Rid areaRid, Area2D area, long areaShapeIndex, long localShapeIndex)
    //{
    //    GD.Print(@"_area_AreaShapeEntered");
    //}

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {

    }

    public override void _PhysicsProcess(double delta)
    {
        base._PhysicsProcess(delta);

        var is0Down = Input.IsActionJustPressed("game_track0");
        var is1Down = Input.IsActionJustPressed("game_track1");
        var is2Down = Input.IsActionJustPressed("game_track2");
        var is3Down = Input.IsActionJustPressed("game_track3");

        if (is0Down || is1Down || is2Down || is3Down)
        {
            foreach (var item in _area.GetOverlappingAreas())
            {
                if (item is TrackBlock trackBlock)
                {
                    if (trackBlock.TrackId == 0 && is0Down ||
                        trackBlock.TrackId == 1 && is1Down ||
                        trackBlock.TrackId == 2 && is2Down ||
                        trackBlock.TrackId == 3 && is3Down)
                    {
                        trackBlock.IsCompleted = true;
                        _blockGenerator.RemoveTrackBlock(trackBlock);
                        _score++;
                     
                    }
                    else
                    {
                        //_missCount++;
                    }

                }
            }
        }

        UpdateLabel();
    }

    private void UpdateLabel()
    {
        _label.Text = $"score: {_score} miss {_missCount}";
    }

    public void _on_button_pressed()
    {
        _blockGenerator.Play();
    }
}
